<template>
  <div class="image-gallery">
    <!-- 使用 Element UI 的菜单组件 -->
    <el-menu
      :default-active="activeCategory"
      mode="horizontal"
      @select="handleSelect"
      class="category-menu"
    >
      <el-menu-item
        v-for="(category, index) in categories"
        :key="index"
        :index="category.name"
      >
        {{ category.name }}
      </el-menu-item>
    </el-menu>

    <!-- 图片展示区 -->
    <div class="image-container">
      <el-card v-if="selectedCategory" class="gallery-card">
        <div slot="header" class="category-header">
          <h3>{{ selectedCategory.name }}</h3>
        </div>
        <el-row :gutter="20">
          <el-col
            v-for="(image, imgIndex) in selectedCategory.images"
            :key="imgIndex"
            :span="8"
            class="image-item"
          >
            <div class="image-wrapper">
              <el-image
                :src="image.url"
                fit="cover"
                class="image"
                :preview-src-list="selectedCategory.images.map(img => img.url)"
                :initial-index="imgIndex"
              >
                <div slot="placeholder" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
                <div slot="error" class="image-slot">
                  <i class="el-icon-picture-outline"></i>
                </div>
              </el-image>
              <!-- 图片信息区域 - 只在悬停时显示 -->
              <div class="image-info">
                <div class="image-name">{{ image.name || '未命名图片' }}</div>
                <div class="image-source" v-if="image.source">
                  <span>来源: </span>
                  <a :href="image.sourceUrl || '#'" target="_blank" v-if="image.sourceUrl">{{ image.source }}</a>
                  <span v-else>{{ image.source }}</span>
                </div>
              </div>
            </div>
          </el-col>
        </el-row>
      </el-card>
      <el-empty v-else description="请选择一个分类" class="empty-state"></el-empty>
    </div>
  </div>
</template>

<script>
import {getCategoryImages} from "@/api/kiku/pic";

export default {
  data() {
    return {
      categories: [
      ],
      activeCategory: "小清新风格",
      selectedCategory: null,
    };
  },
  methods: {
    getList() {
      getCategoryImages().then(res => {
        this.categories = res.data.categories;

        // 等拿到数据后再设置当前分类
        this.selectedCategory = this.categories.find(
          cat => cat.name === this.activeCategory
        );
      });
    },
    handleSelect(index) {
      this.activeCategory = index;
      this.selectedCategory = this.categories.find((cat) => cat.name === index);
    }
  },
  created() {
    this.getList();
    this.selectedCategory = this.categories.find(
      (cat) => cat.name === this.activeCategory
    );
  },
};
</script>

<style scoped>
.image-gallery {
  max-width: 1200px;
  margin: 40px auto;
  padding: 20px;
  background-color: #fff;
  border-radius: 8px;
}

.category-menu {
  margin-bottom: 30px;
  display: flex;
  justify-content: center;
}

.image-container {
  margin-top: 10px;
}

.gallery-card {
  border-radius: 8px;
}

.category-header h3 {
  font-size: 16px;
  font-weight: 500;
  color: #333;
  margin: 0;
}

.image-item {
  margin-bottom: 20px;
  border-radius: 6px;
  overflow: hidden;
  aspect-ratio: 4/3;
  cursor: pointer;
  transition: all 0.3s ease;
}

.image-wrapper {
  position: relative;
  width: 100%;
  height: 100%;
}

.image {
  width: 100%;
  height: 100%;
  object-fit: cover;
  border-radius: 4px;
  transition: all 0.3s ease;
}

.image-info {
  position: absolute;
  bottom: 0;
  left: 0;
  right: 0;
  padding: 8px 12px;
  background-color: rgba(0, 0, 0, 0.7);
  color: white;
  transform: translateY(100%);
  opacity: 0;
  transition: all 0.3s ease;
  border-bottom-left-radius: 4px;
  border-bottom-right-radius: 4px;
}

.image-item:hover {
  transform: translateY(-3px);
  box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
}

.image-item:hover .image-info {
  transform: translateY(0);
  opacity: 1;
}

.image-name {
  font-weight: bold;
  margin-bottom: 4px;
}

.image-source {
  font-size: 12px;
  opacity: 0.8;
}

.image-source a {
  color: #409EFF;
  text-decoration: none;
}

.image-source a:hover {
  text-decoration: underline;
}

.image-slot {
  display: flex;
  justify-content: center;
  align-items: center;
  width: 100%;
  height: 100%;
  background: #f5f7fa;
  color: #909399;
  font-size: 30px;
  border-radius: 4px;
}

.empty-state {
  height: 300px;
  display: flex;
  justify-content: center;
  align-items: center;
  border: 1px dashed #eee;
  border-radius: 8px;
}
</style>
